﻿package com.game.proxy
{
	import com.dusk.encrypt.JSONEncrypt;
	import com.dusk.game.HackChecker;
	import com.dusk.game.Logger;
	import com.dusk.net.NativeFile;
	import com.dusk.util.TimeUtil;
	import com.game.data.constant.ConfigConst;
	import com.game.manager.NotificationManager;
	import com.hurlant.util.Base64;
	
	import config.Config;
	
	import flash.utils.ByteArray;
	
	public class UserDataProxy
	{
		
		public function UserDataProxy()
		{
			this.date = new Date();
			super();
			this.gc = Config.getIns();
		}
		
		public var myStorageObj:Object;
		private var gc:Config;
		private var date:Date;
		
		public function getStorage():void
		{
			var obj:Object = this.myStorageObj;
			if (obj)
			{
				this.gc.playNum = int(obj.playerNum);
				if(obj.gameVersion > ConfigConst.GAME_VERSION_ID) HackChecker.hackHandler();
				this.gc.userData.buildFromObject(obj.userData);
				if (obj.saveDate != undefined)
				{
					var _loc3_:int = TimeUtil.getDayBetweenTwoDate(obj.saveDate, this.gc.curDate);
					if (_loc3_ < 0)
					{
						NotificationManager.alert("该存档时间异常！请勿修改时间！");
						return;
					}
					else if (_loc3_ == 0)
					{
						if (obj.allTask != undefined) this.gc.allTask.setAllTask(obj.allTask);
					}
				}
				if (obj.actTask) this.gc.allTask.setActTask(obj.actTask);
				if (obj.flowerSoulInfo) gc.flowerSoul.setSaveObj(obj.flowerSoulInfo);
				if (obj.playerData != undefined)
				{
					this.gc.playerData.importFromSaveData(obj.player1_obj);
				}
			}
		}//将mystorage数据读取分析
		
		public function setStorage():void
		{
			this.myStorageObj = {};
			this.myStorageObj.playerNum = this.gc.playNum;
			this.myStorageObj.saveDate = this.gc.curDate;
			this.myStorageObj.gameVersion = ConfigConst.GAME_VERSION_ID;
			this.myStorageObj.userData = this.gc.userData.cloneToObject();
			this.myStorageObj.allTask = this.gc.allTask.saveAllTask();
			this.myStorageObj.actTask = this.gc.allTask.saveActionTask();
			this.myStorageObj.flowerSoulInfo = this.gc.flowerSoul.getSaveObj();
			this.myStorageObj.playerData = this.gc.playerData.exportToSaveData();
		}//将游戏数据集成写入可读取对象
		
		public function storageValue(param2:*):void
		{
			this.myStorageObj = param2;
		}
		
		public function saveGame(param1:int, isAlert:Boolean = false):void
		{
			if (param1 == -1)
			{
				NotificationManager.alert("存档失败！请先选择存档！");
				return;
			}
			this.setStorage();
			var encrypted:String = handleGameData(this.myStorageObj, "en");
			var url:String = NativeFile.getAppFolderFileUrl("gameData/" + param1 + ".sav");
			var isSuccess:Boolean = NativeFile.write(encrypted, url);
			if (!isSuccess)
			{
				NotificationManager.alert("存档失败！请检查存档目录是否有写入权限！");
				return;
			}
			if (isAlert)
			{
				NotificationManager.alert("存档成功！")
			}
		}
		
		public function getAllSaveData():Object
		{
			var allGameData:Object = {};
			for (var idx:int = 0; idx < 6; idx++)
			{
				var url:String = NativeFile.getAppFolderFileUrl("gameData/" + idx + ".sav");
				if (NativeFile.exists(url))
				{
					allGameData[idx] = NativeFile.read(url);
					allGameData[idx] = handleGameData(allGameData[idx], "de");
				}
				else
				{
					Logger.log("未找到存档" + (idx + 1) + "数据");
				}
			}
			return allGameData;
		}
		
		/**
		 * 处理游戏数据
		 */
		public function handleGameData(param1:*, param2:String):*
		{
			if (!param1)
			{
				return null;
			}
			var i:int = 0;
			var ba:ByteArray = new ByteArray();
			switch (param2)
			{
				case "de":
					ba = Base64.decodeToByteArray(param1);
					for (i = 0; i < 10; ++i)
					{
						ba.uncompress();
					}
					return JSONEncrypt.decryptObject(ba.readObject()) as Object;
				case "en":
					ba.writeObject(JSONEncrypt.encryptObject(param1));
					for (i = 0; i < 10; ++i)
					{
						ba.compress();
					}
					return Base64.encodeByteArray(ba) as String;
			}
		}
	}
}